package tasks;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.transaction.annotation.Transactional;

import repository.UserRepository;
import repository.impl.UserRepositoryImpl;
import util.ApiClientBuilder;
import util.FacebookMatchHelper;
import util.LinkedInMatchHelper;

import com.google.code.linkedinapi.client.LinkedInApiClientException;

import domain.User;

public class SocialUpdateJob extends QuartzJobBean {
	private int timeout;
	  
  /**
   * Setter called after the SocialUpdateJob is instantiated
   * with the value from the JobDetailBean
   */ 
  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }
  
  @SuppressWarnings("unchecked")
  @Transactional(readOnly = false)
protected void executeInternal(JobExecutionContext ctx)
  throws JobExecutionException {
	  final String LINKEDIN_API_KEY = "rnaeme79uqz5";
	  final String LINKEDIN_API_SECRET = "nz5A3kPcwP0yzkfs";
	  final String FACEBOOK_API_KEY = "429147717173757";
	  final String FACEBOOK_API_SECRET = "3d822d5a2feda7a4dc390cb3e0ced38f";

	  SessionFactory sessionFactory = (SessionFactory) ctx.getMergedJobDataMap().get("sessionFactory");
	  final LinkedInMatchHelper linkedInMatchHelper = (LinkedInMatchHelper) ctx.getMergedJobDataMap().get("linkedInMatchHelper");
	  final FacebookMatchHelper facebookMatchHelper = (FacebookMatchHelper) ctx.getMergedJobDataMap().get("facebookMatchHelper");
	  final UserRepository userRepository = (UserRepository) ctx.getMergedJobDataMap().get("userRepository");
	  
	  
	  HibernateTemplate template = new HibernateTemplate(sessionFactory, true);
	  template.executeWithNewSession(new HibernateCallback()
	  {
	      @Override
	      public Object doInHibernate(Session session) throws HibernateException, SQLException
	      {
	    	  List<User> users = userRepository.getRegularConfirmedUsers();
	    	  User adminSocialUser = userRepository.getAdminUserWithSocialToken();
	    	  
	    	
	    	  ApiClientBuilder apiClientBuilder = new ApiClientBuilder();

	    	  if (apiClientBuilder.buildLinkedInApiClient(adminSocialUser,
	    			  LINKEDIN_API_KEY, LINKEDIN_API_SECRET)) {
	    	  	try {
	      			linkedInMatchHelper.match(apiClientBuilder, users);
	    	  	} catch (LinkedInApiClientException e) {
	    	  	}	
	    	  }
	    	  
	    	  if (apiClientBuilder.buildFacebookClient(adminSocialUser, FACEBOOK_API_KEY, FACEBOOK_API_SECRET)) {
	    		  try {
	    	  			facebookMatchHelper.match(apiClientBuilder, users);
	    		  	} catch (Exception e) {
	    		  	}  
	    	  }
	          
	          return null;
	      }
	  });
	  
  }
}
